﻿// Copyright (c) JosephZheng. All rights reserved. See License.txt in the project root for license

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using MessageBoxHelperLib;

namespace Lyra.Cache.HelperTool.SubForm
{
    public partial class SetCacheForm : Form
    {
        public object CacheObject { get; private set; }
        public Type CacheType { get; private set; }
        private Dictionary<string, PropertyInfo> Propertys { get; set; }

        public SetCacheForm(string key, Type type, Dictionary<string, string> values)
        {
            InitializeComponent();
            this.tstbKey.Text = key;
            this.CacheType = type;
            this.Propertys = new Dictionary<string, PropertyInfo>();
            foreach (PropertyInfo pi in type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.SetProperty))
            {
                this.Propertys.Add(pi.Name, pi);
                if (values.ContainsKey(pi.Name))
                {
                    if (pi.PropertyType == typeof(string))
                    {
                        this.dgvMain.Rows.Add(pi.Name, "System.String", values[pi.Name]);
                    }
                    else if (pi.PropertyType.IsGenericType && pi.PropertyType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
                    {
                        this.dgvMain.Rows.Add(pi.Name, string.Concat(new NullableConverter(pi.PropertyType).UnderlyingType.FullName, "?"), values[pi.Name]);
                    }
                }
                else
                {
                    if (pi.PropertyType == typeof(string))
                    {
                        this.dgvMain.Rows.Add(pi.Name, "System.String");
                    }
                    else if (pi.PropertyType.IsGenericType && pi.PropertyType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
                    {
                        this.dgvMain.Rows.Add(pi.Name, string.Concat(new NullableConverter(pi.PropertyType).UnderlyingType.FullName, "?"));
                    }
                }
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(this.tstbKey.Text))
            {
                bool hasvalue = false;
                object cache = Activator.CreateInstance(CacheType);
                foreach (DataGridViewRow row in this.dgvMain.Rows)
                {
                    if (!string.IsNullOrWhiteSpace(row.Cells[2].Value as string))
                    {
                        try
                        {
                            object value = null;
                            string typename = row.Cells[1].Value.ToString();
                            if (typename == "System.String")
                            {
                                value = row.Cells[2].Value.ToString();
                            }
                            else
                            {
                                value = Convert.ChangeType(row.Cells[2].Value.ToString(), Type.GetType(typename.Substring(0, typename.Length - 1)));
                            }
                            this.Propertys[row.Cells[0].Value.ToString()].SetValue(cache, value, null);
                            hasvalue = true;
                        }
                        catch
                        {
                            MessageBoxHelper.ShowWarning("wrong value!");
                            return;
                        }
                    }
                }
                if (hasvalue)
                {
                    CacheObject = cache;
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                }
                else
                {
                    MessageBoxHelper.ShowInfo("no value set!");
                }
            }
            else
            {
                MessageBoxHelper.ShowInfo("key is empty!");
            }
        }
    }
}
